package org.ling.xuan.table;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.ListTableModel;
import org.jetbrains.annotations.Nullable;
import org.ling.xuan.entity.mo.LocalAdd;

import java.util.List;

public class LocalAddTableModel extends ListTableModel<LocalAdd> {
    private List<LocalAdd> localAddList;
    private static final String[] columnNames = {"ID", "Modify ID", "Add Message", "Commit", "Send", "Require Bank", "Requirement ID","Ctd Time", "Requirement Description"};
    private static final ColumnInfo[] columnInfos =new ColumnInfo[columnNames.length];
    static {
        for (int i = 0; i < columnNames.length; i++) {
            ColumnInfo<LocalAdd,String> columnInfo = new ColumnInfo<>(columnNames[i]) {
                @Override
                public @Nullable String valueOf(LocalAdd localAdd) {
                    return localAdd.getId();
                }
                @Override
                public boolean isCellEditable(LocalAdd localAdd) {
                    return true;
                }
            };
            columnInfos[i]=columnInfo;
        }
    }
    public LocalAddTableModel(List<LocalAdd> localAddList) {
        super(columnInfos,localAddList);
        this.localAddList = localAddList;
    }

    @Override
    public int getRowCount() {
        return localAddList.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "ID";
            case 1: return "Modify ID";
            case 2: return "Add Message";
            case 3: return "Commit";
            case 4: return "Send";
            case 5: return "Require Bank";
            case 6: return "Requirement ID";
            case 7: return "Ctd Time";
            case 8: return "Requirement Description";
            default: return super.getColumnName(column);
        }
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        LocalAdd localAdd = localAddList.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return localAdd.getId();
            case 1:
                return localAdd.getModifyId();
            case 2:
                return localAdd.getAddMsg();
            case 3:
                return String.valueOf(localAdd.isCommited());
            case 4:
                return String.valueOf(localAdd.isSend());
            case 5:
                return localAdd.getRequireBank();
            case 6:
                return localAdd.getRequirementId();
            case 7:
                return DateUtil.format(localAdd.getCtdTime(), DatePattern.NORM_DATETIME_PATTERN);
            case 8:
                return localAdd.getRequirementDes();
            default:
                return null;
        }
    }

    public void removeRow(int rowIndex) {
        localAddList.remove(rowIndex);
        fireTableRowsDeleted(rowIndex, rowIndex);
    }

    public void refreshData(List<LocalAdd> list) {
        this.localAddList = list;
    }
}
